/*
package my.mark.mybaibaoxiang.nhr.codeToName;

import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import com.nhr.bm.sdk.common.util.CollectionUtil;
import com.nhr.bm.sdk.common.util.StringUtil;
import com.nhr.bm.sdk.modules.configcenter.annotation.CodeMapping;
import com.nhr.bm.sdk.modules.configcenter.annotation.EnumMapping;
import com.nhr.bm.sdk.modules.configcenter.annotation.OptionMapping;
import com.nhr.bm.sdk.modules.configcenter.annotation.WebResponseAdvice;
import com.nhr.bm.sdk.modules.configcenter.entity.BmOptionItemDTO;
import com.nhr.bm.sdk.modules.configcenter.sdk.config.CodeConvertConfig;
import com.nhr.bm.sdk.modules.configcenter.sdk.service.BmOptionSdkService;
import com.nhr.bm.sdk.modules.configcenter.sdk.service.CodeConvertFactory;
import com.nhr.bm.sdk.modules.configcenter.sdk.service.CodeConvertService;
import com.nhr.panda.common.enums.EnumInterface;
import com.nhr.panda.common.page.NhrPagination;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

*/
/**
 * @author wxming.
 * @date 2024/7/13.
 * @Description 码表和枚举code转描述
 *//*

@Component
public class CodeConvertAbilityProvider {
    private static final Logger log = LoggerFactory.getLogger(CodeConvertAbilityProvider.class);
    @Autowired
    private BmOptionSdkService bmOptionSdkService;
    @Autowired
    private CodeConvertConfig codeConvertConfig;

    public CodeConvertAbilityProvider() {
    }

    public void convert(Object object) {
        this.convert(object, 5);
    }

    public void convert(Object object, int maxDepthLimit) {
        long startTime = System.currentTimeMillis();

        try {
            if (object instanceof NhrPagination) {
                this.wrapperMultiResponseDTO(((NhrPagination)object).getList(), 0, maxDepthLimit);
            } else if (object instanceof List) {
                this.wrapperMultiResponseDTO((List)object, 0, maxDepthLimit);
            } else {
                this.wrapperSingleResponseDTO(object, 0, maxDepthLimit);
            }
        } finally {
            CodeConvertThreadLocal.clear();
        }

        long endTime = System.currentTimeMillis();
        log.info("CodeConvertAbilityProvider execute convert complete, cost time: ({}) timeMillis", endTime - startTime);
    }

    protected void convert(Object object, int maxDepthLimit, WebResponseAdvice webResponseAdvice) {
        if (Objects.isNull(CodeConvertThreadLocal.getEnableLocalCache())) {
            CodeConvertThreadLocal.enableLocalCache(webResponseAdvice.enableLocalCache());
        }

        if (Objects.isNull(CodeConvertThreadLocal.getRpcRetry())) {
            CodeConvertThreadLocal.rpcRetry(webResponseAdvice.rpcRetry());
        }

        this.convert(object, maxDepthLimit);
    }

    private void wrapperMultiResponseDTO(List<Object> objects, Integer depth, Integer maxDepthLimit) {
        if (!CollectionUtil.isEmpty(objects)) {
            objects.stream().filter((object) -> {
                return Objects.nonNull(object);
            }).forEach((object) -> {
                this.wrapperSingleResponseDTO(object, depth + 1, maxDepthLimit);
            });
        }
    }

    private void wrapperSingleResponseDTO(Object object, Integer depth, Integer maxDepthLimit) {
        if (!Objects.isNull(object)) {
            if (depth > maxDepthLimit) {
                log.warn("Exceeds the maximum depth ({}) of data processing", maxDepthLimit);
            } else {
                Class<?> objectClass = object.getClass();
                Field[] fields = ReflectUtil.getFields(objectClass);
                if (fields != null && fields.length != 0) {
                    Map<String, OptionMapping> field4OptionCodeMap = new HashMap();
                    Arrays.asList(fields).stream().filter((fieldx) -> {
                        return fieldx.isAnnotationPresent(OptionMapping.class);
                    }).forEach((fieldx) -> {
                        OptionMapping optionMapping = (OptionMapping)fieldx.getAnnotation(OptionMapping.class);
                        field4OptionCodeMap.put(optionMapping.codeFieldName(), optionMapping);
                    });
                    Set<String> fieldSet = new HashSet();
                    Field[] var8 = fields;
                    int var9 = fields.length;

                    for(int var10 = 0; var10 < var9; ++var10) {
                        Field field = var8[var10];
                        if (!fieldSet.contains(field.getName())) {
                            fieldSet.add(field.getName());
                            field.setAccessible(true);
                            if (field.getType() == List.class) {
                                List<Object> listObjects = (List)ReflectUtil.getFieldValue(object, field);
                                if (CollectionUtil.isEmpty(listObjects)) {
                                    continue;
                                }

                                listObjects.stream().forEach((listObject) -> {
                                    this.wrapperSingleResponseDTO(listObject, depth + 1, maxDepthLimit);
                                });
                            }

                            if (field.isAnnotationPresent(EnumMapping.class)) {
                                EnumMapping enumMapping = (EnumMapping)field.getAnnotation(EnumMapping.class);
                                ReflectUtil.setFieldValue(object, field, this.getEnumMsg(enumMapping, object));
                            } else if (!field.isAnnotationPresent(OptionMapping.class)) {
                                if (field.isAnnotationPresent(CodeMapping.class)) {
                                    CodeMapping codeMapping = (CodeMapping)field.getAnnotation(CodeMapping.class);
                                    ReflectUtil.setFieldValue(object, field, this.getCodeName(codeMapping, object));
                                } else if (this.isNHRBeanObject(field)) {
                                    Object fieldObject = ReflectUtil.getFieldValue(object, field);
                                    if (fieldObject != null) {
                                        this.wrapperSingleResponseDTO(fieldObject, depth + 1, maxDepthLimit);
                                    }
                                }
                            } else {
                                OptionMapping optionMapping = (OptionMapping)field.getAnnotation(OptionMapping.class);
                                if (optionMapping.multiple() && StringUtil.isEmpty(optionMapping.parentFieldName())) {
                                    String optionName = this.findMultipleCOptionName(object, optionMapping);
                                    ReflectUtil.setFieldValue(object, field, optionName);
                                } else {
                                    Optional<BmOptionItemDTO> optionItemDTOOptional = this.findOptionItem(field4OptionCodeMap, object, optionMapping);
                                    if (optionItemDTOOptional.isPresent()) {
                                        ReflectUtil.setFieldValue(object, field, ((BmOptionItemDTO)optionItemDTOOptional.get()).getName());
                                    }
                                }

                                Object objFieldValue = ReflectUtil.getFieldValue(object, field);
                                String fieldValue = null != objFieldValue ? (String)objFieldValue : "";
                                if (optionMapping.defaultIfEmpty() && StringUtils.isBlank(fieldValue)) {
                                    String codeFieldValue = (String)ReflectUtil.getFieldValue(object, optionMapping.codeFieldName());
                                    ReflectUtil.setFieldValue(object, field, codeFieldValue);
                                }
                            }
                        }
                    }

                }
            }
        }
    }

    private Object getCodeName(CodeMapping codeMapping, Object object) {
        String codeIdentification = codeMapping.codeIdentification();
        CodeConvertService codeConvertService = CodeConvertFactory.getServiceByType(codeIdentification);
        if (null == codeConvertService) {
            return null;
        } else {
            String codeFieldName = codeMapping.codeFieldName();
            String codeValue = (String)ReflectUtil.getFieldValue(object, codeFieldName);
            Object codeValueList;
            if (codeMapping.multiple()) {
                codeValueList = StrUtil.split(codeValue, codeMapping.codeSeparator(), true, true);
            } else {
                codeValueList = Lists.newArrayList(new String[]{codeValue});
            }

            return ((List)codeValueList).stream().map((code) -> {
                return CodeConvertControl.enableLocalCache(this.codeConvertConfig) ? (String)CodeLocalCache.get("CODE", code, () -> {
                    return codeConvertService.code2Name(code);
                }) : codeConvertService.code2Name(code);
            }).collect(Collectors.joining(codeMapping.descSeparator()));
        }
    }

    private String getEnumMsg(EnumMapping enumMapping, Object object) {
        String codeFieldName = enumMapping.codeFieldName();
        Object codeValue = ReflectUtil.getFieldValue(object, codeFieldName);
        Class<? extends EnumInterface> enumClass = enumMapping.enumClass();
        EnumInterface[] enumConstants = (EnumInterface[])enumClass.getEnumConstants();
        List<Object> codeValues = new ArrayList();
        if (enumMapping.multiple() && codeValue instanceof String) {
            codeValues.addAll(Arrays.asList(StringUtil.emptyIfNull((String)codeValue).split(enumMapping.codeSeparator())));
        } else {
            codeValues.add(codeValue);
        }

        return (String)codeValues.stream().map((enumCode) -> {
            EnumInterface[] var2 = enumConstants;
            int var3 = enumConstants.length;

            for(int var4 = 0; var4 < var3; ++var4) {
                EnumInterface enumConstant = var2[var4];
                if (Objects.equals(enumConstant.getEnumCode(), enumCode)) {
                    return enumConstant.getEnumMsg();
                }
            }

            return "";
        }).collect(Collectors.joining(enumMapping.descSeparator()));
    }

    private String findMultipleCOptionName(Object object, OptionMapping optionMapping) {
        if (!StringUtil.isEmpty(optionMapping.optionCode()) && !StringUtil.isEmpty(optionMapping.codeFieldName())) {
            String codeFieldValue = (String)ReflectUtil.getFieldValue(object, optionMapping.codeFieldName());
            return (String)Arrays.stream(StringUtil.emptyIfNull(codeFieldValue).split(optionMapping.codeSeparator())).map((codeVal) -> {
                (new StringBuilder()).append(optionMapping.optionCode()).append(":").append(codeFieldValue).toString();
                Optional<BmOptionItemDTO> optionItemOptional = this.bmOptionSdkService.findOneCOptionItem(optionMapping.optionCode(), codeVal);
                return optionItemOptional.isPresent() ? ((BmOptionItemDTO)optionItemOptional.get()).getName() : "";
            }).collect(Collectors.joining(optionMapping.descSeparator()));
        } else {
            return null;
        }
    }

    private Optional<BmOptionItemDTO> findOptionItem(Map<String, OptionMapping> field4OptionCodeMap, Object object, OptionMapping optionMapping) {
        if (StringUtil.isEmpty(optionMapping.optionCode()) || StringUtil.isEmpty(optionMapping.codeFieldName())) {
            Optional.absent();
        }

        String codeFieldValue = (String)ReflectUtil.getFieldValue(object, optionMapping.codeFieldName());
        if (StringUtil.isEmpty(codeFieldValue)) {
            Optional.absent();
        }

        (new StringBuilder()).append(optionMapping.optionCode()).append(":").append(codeFieldValue).toString();
        String parentFieldName = optionMapping.parentFieldName();
        if (StringUtil.isEmpty(parentFieldName)) {
            Optional<BmOptionItemDTO> optionItemOptional = this.bmOptionSdkService.findOneCOptionItem(optionMapping.optionCode(), codeFieldValue);
            return optionItemOptional;
        } else if (!field4OptionCodeMap.containsKey(parentFieldName)) {
            return Optional.absent();
        } else {
            OptionMapping parentOptionMapping = (OptionMapping)field4OptionCodeMap.get(parentFieldName);
            Optional<BmOptionItemDTO> parentOptional = this.findOptionItem(field4OptionCodeMap, object, parentOptionMapping);
            if (parentOptional.isPresent()) {
                Long parentOptionItemId = ((BmOptionItemDTO)parentOptional.get()).getId();
                Optional<BmOptionItemDTO> optionItemOptional = this.bmOptionSdkService.findOneROptionItem(optionMapping.optionCode(), codeFieldValue, parentOptionItemId);
                return optionItemOptional;
            } else {
                return Optional.absent();
            }
        }
    }

    private Field[] getAllDeclaredFields(Class<?> objectClass) {
        Class<?> superclass = null;
        ArrayList fields = new ArrayList();

        Field[] allDeclaredFields;
        do {
            allDeclaredFields = objectClass.getDeclaredFields();
            fields.addAll(Arrays.asList(allDeclaredFields));
            superclass = objectClass.getSuperclass();
            objectClass = superclass;
        } while(superclass != null && !superclass.getName().equals("java.lang.Object"));

        allDeclaredFields = new Field[fields.size()];
        return (Field[])fields.toArray(allDeclaredFields);
    }

    private boolean isNHRBeanObject(Field field) {
        Class<?> fieldClass = field.getType();
        return fieldClass.getName().startsWith("com.nhr");
    }
}
*/
